/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.Person_;
import org.ipti.br.tag.entity.QuestionsPerson;
import org.ipti.br.tag.entity.QuestionsPerson_;
import org.ipti.br.tag.entity.Questions_;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author ipti
 */
@Stateless
public class QuestionsPersonFacade extends AbstractFacade<QuestionsPerson> {

    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public QuestionsPersonFacade() {
        super(QuestionsPerson.class);
    }

    @Override
    public void create(QuestionsPerson questionsPerson) {
        getEntityManager().persist(questionsPerson);
    }

    @Override
    public void edit(QuestionsPerson questionsPerson) {
        questionsPerson.setSerialNumber(null);

        getEntityManager().merge(questionsPerson);
    }

    public void editForSynchronization(QuestionsPerson questionsPerson) {
        getEntityManager().merge(questionsPerson);
    }

    public QuestionsPerson findQuestionsPerson(Long questionID, Long personID) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<QuestionsPerson> cq = cb.createQuery(QuestionsPerson.class);
        Root<QuestionsPerson> from = cq.from(QuestionsPerson.class);
        Predicate predicate1 = cb.equal(from.get(QuestionsPerson_.questions).get(Questions_.id), questionID);
        Predicate predicate2 = cb.equal(from.get(QuestionsPerson_.person).get(Person_.id), personID);
        cq.where(cb.and(predicate1, predicate2));
        QuestionsPerson questionsPerson = null;
        try {
            questionsPerson = getEntityManager().createQuery(cq).getSingleResult();
        } catch (Exception e) {
            return null;
        }
        return questionsPerson;
    }

    public List<QuestionsPerson> modifiedQuestionsPersonBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<QuestionsPerson> cq = cb.createQuery(QuestionsPerson.class);
        Root<QuestionsPerson> from = cq.from(QuestionsPerson.class);
        Predicate predicate1 = cb.notEqual(from.get(QuestionsPerson_.serialNumber), serialNumber);
        Predicate predicate2 = cb.greaterThanOrEqualTo(from.get(QuestionsPerson_.lastModified), initialDate);
        Predicate predicate3 = cb.lessThan(from.get(QuestionsPerson_.lastModified), finalDate);
        cq.where(cb.and(predicate1, predicate2, predicate3));

        return getEntityManager().createQuery(cq).getResultList();
    }
}
